<?php

//大数相加
class BigNumberAdd {

    /**
     * 基本实现方法
     * @param string $bigNumberA
     * @param string $bigNumberB
     * @return string
     */
    public function do(string $bigNumberA, string $bigNumberB) {
        //1. 把两个大整数用数组逆序存储, 数组长度等于较大整数位+1
        $maxLength = strlen($bigNumberA) > strlen($bigNumberB) ? strlen($bigNumberA) : strlen($bigNumberB);
        $arrayA = array_fill(0, $maxLength, 0);//默认以0填充
        for ($i = 0; $i < strlen($bigNumberA); $i++) {
            $arrayA[$i] = (int)substr($bigNumberA, strlen($bigNumberA) - 1 - $i, 1);//转换为整数
        }
        $arrayB = array_fill(0, $maxLength, 0);//默认以0填充
        for ($i = 0; $i < strlen($bigNumberB); $i++) {
            $arrayB[$i] = (int)substr($bigNumberB, strlen($bigNumberB) - 1 - $i, 1);//转换为整数
        }

        //2. 构建result数组, 数组长度等于较大整数位数+1
        $result = array_fill(0, $maxLength + 1, 0);//默认以0填充

        //3. 遍历数组, 按位相加
        for ($i = 0; $i < count($result); $i++) {
            $temp = $result[$i] + $arrayA[$i] + $arrayB[$i];
            //判断是否进位
            if ($temp > 10) {
                $temp -= 10;
                $result[$i + 1] = 1;//默认值是0, 进位后下一位就是1
            }
            $result[$i] = $temp;
        }

        //4. 把result数组再次逆序并转成string
        $rt = strrev(implode('', $result));
        //如果第一位是0, 则从第二位开始取(不可能出现前面两位都是0的情况)
        if (substr($rt, 0, 1) === '0') {
            $rt = substr($rt, 1);
        }

        return $rt;
    }

}

$m = new BigNumberAdd();
echo $m->do('426709752318', '95481253129');